﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleComparePageControlController : ControllerBase
    {
        // GET: api/<RoleComparePageControlController>
        [HttpGet]
        public List<DB.Models.RoleComparePageControl> Get()
        {
            using(DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.RoleComparePageControls
                    .Include(o => o.PageWithControl)
                    .Include(o => o.Role)
                    .ToList();
                return data;
            }
        }

        // GET api/<RoleComparePageControlController>/5
        [HttpGet("{id}")]
        public DB.Models.RoleComparePageControl Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.RoleComparePageControls
                    .Where(o => o.Id == id)
                    .Include(o => o.PageWithControl)
                    .Include(o => o.Role)
                    .SingleOrDefault();
                return data;
            }
        }

        // POST api/<RoleComparePageControlController>
        [HttpPost]
        public void Post([FromBody] DB.Models.RoleComparePageControl value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.RoleComparePageControls.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<RoleComparePageControlController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] DB.Models.RoleComparePageControl value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var roleComparePageControl = db.RoleComparePageControls
                    .Where(o => o.Id == value.Id)
                    .SingleOrDefault();
                if (roleComparePageControl != null)
                {
                    roleComparePageControl.RoleId = value.RoleId;
                    roleComparePageControl.PageWithControlId = value.PageWithControlId;
                    db.SaveChanges();
                }
            }

        }

        // DELETE api/<RoleComparePageControlController>/5
        [HttpDelete("{id}")]
        public void Delete(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var roleComparePageControl = db.RoleComparePageControls
                    .Where(o => o.Id == id)
                    .SingleOrDefault();
                if (roleComparePageControl != null)
                {
                    db.RoleComparePageControls.Remove(roleComparePageControl);
                    db.SaveChanges();
                }
            }
        }
    }
}
